/**
* This file contains functions used by both server.c, client.c, and storage.c
*/
#include "shared.h"

/**
* This function sets up the current time and returns it into strings given by pointers
* @param dateS Current date
* @param hourS Current hour
* @param monthS Current month
* @param minS Current minute
* @param secS Current second
* @param year Current year
*/
void setupTime(char* dateS, char* hourS, char* monthS, char* minS, char* secS, int* year)
{
    time_t rawtime;
    struct tm *timeinfo;
    time(&rawtime); // get current calendar time
    timeinfo = localtime(&rawtime); // Express time in local time zone
    int sec, min, hour, date, month;

    sec = timeinfo->tm_sec;
    if (sec < 10)
	sprintf(secS, "0%d", sec);
    else
	sprintf(secS,"%d", sec);
    min = timeinfo->tm_min;
    if (min < 10)
        sprintf(minS, "0%d", min);
    else
        sprintf(minS,"%d", min);
    hour = timeinfo->tm_hour;
    if (hour < 10)
        sprintf(hourS, "0%d", hour);
    else
        sprintf(hourS,"%d", hour);
    date = timeinfo->tm_mday;
    if (date < 10)
        sprintf(dateS, "0%d", date);
    else
        sprintf(dateS,"%d", date);
    month = 1 + timeinfo->tm_mon;
    if (month < 10)
        sprintf(monthS, "0%d", month);
    else
        sprintf(monthS,"%d", month);
    *year = 1900 + timeinfo->tm_year;
    return;
}
